package com.chenfeng.ego.service.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.chenfeng.ego.commons.pojo.EasyUIDataGrid;
import com.chenfeng.ego.commons.util.IDUtils;
import com.chenfeng.ego.commons.util.ItemSpiltUtil;
import com.chenfeng.ego.mapper.TbItemMapper;
import com.chenfeng.ego.mapper.TbItemParamMapper;
import com.chenfeng.ego.pojo.TbItem;
import com.chenfeng.ego.pojo.TbItemExample;
import com.chenfeng.ego.pojo.TbItemParam;
import com.chenfeng.ego.service.TBItemService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mysql.jdbc.Util;

@Service
public class TBItemServiceImpl implements TBItemService
{
	@Resource
	private TbItemMapper tbItemMapper;
	@Resource
	private TbItemParamMapper itemParamMapper;
	//商品列表
	public EasyUIDataGrid showTbItem(int page, int rows)
	{
		EasyUIDataGrid dataGrid = new EasyUIDataGrid();
		// 设置分页条件
		PageHelper.startPage(page, rows);
		// 查询分页结果
		List<TbItem> list = tbItemMapper.selectByExample(new TbItemExample());
		PageInfo<TbItem> info = new PageInfo<>(list);
		dataGrid.setRows(info.getList());
		dataGrid.setTotal(info.getTotal());
		return dataGrid;
	}
	//自己写的
	/*
	public int deleteTbItem(String[] str)
	{  
	int flag = 0;
	for (String string2 : str)
	{
		TbItem tbItem = new TbItem();
		tbItem.setStatus(Byte.valueOf("3"));
		TbItemExample tbItemExample = new TbItemExample();
		tbItemExample.createCriteria().andIdEqualTo(Long.valueOf(string2));
		int i =tbItemMapper.updateByExampleSelective(tbItem,tbItemExample );
			System.out.println(i);
			if (i > 0)
			{
				flag = 200;
				return flag;
			}
		}
		return flag;
	}
	@Override
	public int instockTbItem(String[] str)
	{
		int flag = 0;
		for (String string2 : str)
		{
			TbItem tbItem = new TbItem();
			tbItem.setStatus(Byte.valueOf("2"));
			TbItemExample tbItemExample = new TbItemExample();
			tbItemExample.createCriteria().andIdEqualTo(Long.valueOf(string2));
			int i =tbItemMapper.updateByExampleSelective(tbItem,tbItemExample );
			System.out.println(i);
			if (i > 0)
			{
				flag = 200;
				return flag ;
			}
		}
		return flag;
	}
	@Override
	public int reshelfTbItem(String[] str)
	{
		int flag = 0;
		for (String string2 : str)
		{
			TbItem tbItem = new TbItem();
			tbItem.setStatus(Byte.valueOf("1"));
			TbItemExample tbItemExample = new TbItemExample();
			tbItemExample.createCriteria().andIdEqualTo(Long.valueOf(string2));
			int i =tbItemMapper.updateByExampleSelective(tbItem,tbItemExample );
			System.out.println(i);
			if (i > 0)
			{
				flag = 200;
				return flag ;
			}
		}
		return flag;
	}*/
	//修改上架 下架 删除  实际上是修改状态
	@Override
	public void upStatus(String ids, byte status) throws Exception
	{
		TbItem record = new TbItem();
		List<Long> list = ItemSpiltUtil.idSplit(ids);
		for (Long long1 : list)
		{
			record.setStatus(status);
			record.setId(long1);
			tbItemMapper.updateByPrimaryKeySelective(record);
		}	
	}
	//商品入库
	@Override
	public int insertByItemAndParams(TbItem itema, String param)throws Exception
	{
		//商品添加	
		Long genId = IDUtils.genItemId();
		itema.setStatus((byte)1);
		itema.setCreated(new Date());
		itema.setUpdated(new Date());
		itema.setId(genId);
		//商品参数添加
		TbItemParam tb = new TbItemParam();
		tb.setCreated(new Date());
		tb.setId(IDUtils.genItemId());
		tb.setItemCatId(genId);
		tb.setParamData(param);
		tb.setUpdated(new Date());
		return tbItemMapper.insert(itema)+itemParamMapper.insert(tb);
	}
}
